$('.available i').on('click', function () {
    if ($(this).css('backgroundPosition') == "-10px -10px") {
        $(this).css('backgroundPosition', '-46px -10px')
    } else {
        $(this).css('backgroundPosition', '-10px -10px')
    }
});

// 商品选中
// function on_selected () {
//      // 或取数量
//     alert('点击一次加')
//     var sku_id = $(this).prev().val(); //商品id
//     // var sku_id = $(this).val(); //商品id
//     alert(sku_id);
//     // var count = $(this).prev().prev().val(); //商品数量
//     // var selected = $("#" + sku_id).prop("checked");  // 获取当前商品的选中状态
//     // alert(selected);
// }

// 购物车+  的实现
function on_jia(th) {
    // 或取数量
    alert('您又买了一件哦～好开心呢！');
    var sku_id = $(th).prev().val(); //商品id
    var count = $(th).prev().prev().val(); //商品数量
    count = Number(count) + 1;
    var selected = $("#" + sku_id).prop("checked");  // 获取当前商品的选中状态
    var whatever = {
        'count': String(count),
        'sku_id': sku_id,
        'selected': selected,
    };
    $.ajax({
        url: '/Shopping/',
        type: "PUT",
        data: JSON.stringify(whatever),
        headers: {
            'X-CSRFToken': getCookie("csrftoken"),
            "Content-Type": "application/json",
            "X-HTTP-Method-Override": "PUT"
        },
    }).done(function (data) {
        console.log('done');
        if (data.code == 0) {
            alert("更改成功");
            window.location.reload()
        } else {
            console.log(data.code);
            console.log(data.errmsg);
            // alert(data.errmsg);
        }

    });
};

// cart -
function on_jian(th) {
    // 获取数量
    alert('点击一次减');
    var count = $(th).next().val(); //商品id
    var sku_id = $(th).next().next().val(); //商品数量c

    if (count == 1) {
        alert("亲，最少买一个啦")
        return;
    } else {
        count = Number(count) - 1;
    }
    var selected = $("#" + sku_id).prop("checked");  // 获取当前商品的选中状态
    var whatever = {
        'count': String(count),
        'sku_id': sku_id,
        'selected': selected,
    };
    $.ajax({
        url: '/Shopping/',
        type: "PUT",
        data: JSON.stringify(whatever),
        headers: {
            'X-CSRFToken': getCookie("csrftoken"),
            "Content-Type": "application/json",
            "X-HTTP-Method-Override": "PUT"
        },
    }).done(function (data) {
        console.log('done');
        if (data.code == 0) {
            alert("更改成功");
            window.location.reload()
        } else {
            console.log(data.code);
            console.log(data.errmsg);
            // alert(data.errmsg);
        }

    });
};

//删除购物车商品
function on_delete(th) {

    $.ajax({
        url: '/delete/',
        dataType: 'json',
        headers: {'X-CSRFToken': getCookie("csrftoken")},
        type: "post",
        data: {
            // __method:"DELETE",
            sku_id: $(th).attr('sku_id'),
        }

    }).done(function (data) {
        console.log('done');
        if (data.code == 0) {
            alert("删除成功");
            window.location.reload()
        } else {
            console.log(data.errmsg);
        }


    }).fail(function () {
        console.log('fail')
    })
};

// 选中
function on_selected(th) {
    var selected = $(th).prop("checked");  // 获取选中状态
    var sku_id = $(th).prop("id"); // 商品id
    var count = $(th).next().val();  // 获取购物车商品数量

    var whatever = {
        'count': String(count),
        'sku_id': sku_id,
        'selected': selected,
    };
    $.ajax({
        url: '/Shopping/',
        type: "PUT",
        data: JSON.stringify(whatever),
        headers: {
            'X-CSRFToken': getCookie("csrftoken"),
            "Content-Type": "application/json",
            "X-HTTP-Method-Override": "PUT"
        }
    }).done(function (data) {
        console.log('done');
        if (data.code == 0) {
            alert("更改成功");
            if (data.cart_sku['selected']) {
                alert("选中状态")
                $(th).attr("checked", "checked");  // 获取选中状态
            }else {
                alert("未选中状态")
                $(th).removeAttr("checked", );  // 获取选中状态
            }

            window.location.reload()
        } else {
            console.log(data.code);
            console.log(data.errmsg);
            // alert(data.errmsg);
        }


    })


}

// 全选
function on_selected_all(th) {
    var selected = $(th).prop("checked");  // 获取选中状态
    var whatever = {
        "selected":selected
    };
    $.ajax({
        url: '/SelectedAll/',
        type: "PUT",
        data: JSON.stringify(whatever),
        headers: {
            'X-CSRFToken': getCookie("csrftoken"),
            "Content-Type": "application/json",
            "X-HTTP-Method-Override": "PUT"
        }
    }).done(function (data) {
        console.log('done');
        if (data.code == 0) {
            alert("更改成功");
            if (data.selected) {
                alert("选中状态");
            }else {
                alert("未选中状态");
            }
            window.location.reload()
        } else {
            console.log(data.code);
            console.log(data.errmsg);
            // alert(data.errmsg);
        }
    })

}
